﻿using DataType.Actions;
using DataType.JudgeResults;
using OprsSimulator.Operators;
using System.Collections.Generic;
using MapCalclator;
using System;
using DataType.CustomEnumType;
using System.Diagnostics;
using OprsSimulator.OprAttributes;

namespace ActionsJudge
{
    internal static class WeaponUnfoldJudge
    {
        public static List<IJudgeResult> ToJudge(IBasicAction acts, List<IOperator> oprs, Map map)
        {
            List<IJudgeResult> judgeResults = new List<IJudgeResult>();

            WeaponUnfoldAction action = (WeaponUnfoldAction)acts;
            IOperator opr = null;
            foreach (IOperator oper in oprs)
            {
                if (oper.ObjID == action.ObjID)
                {
                    opr = oper;
                    break;
                }
            }
            if (opr == null)
            {
                Trace.TraceWarning($"无效裁决：态势中没有ID为{action.ObjID}的算子");
                return new List<IJudgeResult>();
            }
            if (action.Type != ActionType.WeaponUnfold)
            {
                Trace.TraceWarning("无效裁决：算子的动作类型有误，应该是武器展开类型");
                return new List<IJudgeResult>();
            }
            if (!typeof(ICommonAttris).IsAssignableFrom(opr.GetType()) ||
                !typeof(IMoveAttris).IsAssignableFrom(opr.GetType()) ||
                !typeof(IAttackAttris).IsAssignableFrom(opr.GetType()))
            {
                Trace.TraceWarning($"无效裁决：算子{action.ObjID}不具有停展开武器所需要的全部属性");
                return new List<IJudgeResult>();
            }

            int weapon_unfold_state = (int)opr.GetType().GetProperty("WeaponUnfoldState").GetValue(opr);
            int speed = (int)opr.GetType().GetProperty("Speed").GetValue(opr);
            bool stop = (bool)opr.GetType().GetProperty("Stop").GetValue(opr);
            int move_to_stop_remain_time = (int)opr.GetType().GetProperty("MoveToStopRemainTime").GetValue(opr);
            int weapon_unfold_time = (int)opr.GetType().GetProperty("WeaponUnfoldTime").GetValue(opr);
            int get_on_remain_time = (int)opr.GetType().GetProperty("GetOnRemainTime").GetValue(opr);
            int get_off_remain_time = (int)opr.GetType().GetProperty("GetOffRemainTime").GetValue(opr);
            OprType type = (OprType)opr.GetType().GetProperty("Type").GetValue(opr);

            bool weapon_unfold_flag = false;
            if (weapon_unfold_state == 0 && type == OprType.Vehicle)
            {
                if (stop & speed <= 0 & move_to_stop_remain_time <= 0 & weapon_unfold_time <= 0 & get_on_remain_time <= 0 & get_off_remain_time <= 0)
                {
                    weapon_unfold_flag = true;
                }
            }

            WeaponUnfoldJudgeResult weaponUnfoldJudgeResult = new WeaponUnfoldJudgeResult(action.ObjID, weapon_unfold_flag);
            judgeResults.Add(weaponUnfoldJudgeResult);

            return judgeResults;
        }
    }
}
